<?php

function get_current_url() {
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

function article_link($title_link = NULL, $ext = '.html') {
    return base_url() . $title_link . $ext;
}

function cat_link($name = NULL, $prefix = '', $ext = '/') {
    return base_url() . $prefix . $name . $ext;
}

function library_link($name = NULL, $prefix = 'thu-vien/', $ext = '/') {
    return base_url() . $prefix . $name . $ext;
}

function event_link($event_link = NULL, $prefix = 'dong-su-kien', $ext = '') {
    return base_url() . $prefix . '/' . $event_link . $ext;
}

function rss_link($val, $prefix = 'rss', $ext = '.rss') {
    return base_url() . $prefix . '/' . $val . $ext;
}

function search_link($val = NULL, $prefix = 'tim-kiem', $ext = '') {
    return base_url() . $prefix . '/' . $val . $ext;
}

function html_link($val = NULL, $prefix = '', $ext = '.html') {
    return base_url() . $prefix . $val . $ext;
}

function create_thumb($img_path, $w, $h) {
    //return base_url().'image/?id='.$id.'&w='.$w.'&h='.$h.'&x='.$x.'&y='.$y;
    //return base_url().'image/?id='.$id.'&w='.$w.'&h='.$h;
    $img_path = preg_replace('/thumb_([0-9]{2,4}\_[0-9]{2,4})/i', 'thumb_' . $w . '_' . $h, $img_path);
    return site_url($img_path);
}

function cr_thumb_common($url, $w, $h) {
    return site_url('image/create_thumb/?url=' . $url . '&w=' . $w . '&h=' . $h);
}

function admin_redirect($uri = '', $method = 'location', $http_response_code = 302) {
    if (!preg_match('#^https?://#i', $uri)) {
        $uri = config_item('admin_uri') . $uri;
        $uri = site_url($uri);
    }

    switch ($method) {
        case 'refresh' : header("Refresh:0;url=" . $uri);
            break;
        default : header("Location: " . $uri, TRUE, $http_response_code);
            break;
    }
    exit;
}

function create_title_link($txt = '')
{
    $txt = utf8_to_ascii($txt);
    $txt = preg_replace('@[^\w]@is', ' ', strtolower($txt));
    $txt = str_replace(' ', '-', trim($txt));
    return $txt;
}